﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyWeb.Model.User;
using MyWeb.Model;
using MyWeb.Services;
using SqlSugar;
using MyWeb.Repository.UnitOfWorks;
using MyWeb.Common.Helper;
using MyWeb.Model.Models;
using StackExchange.Profiling.Internal;
using MyWeb.Extensions.ServiceExtensions.Swagger;
using static MyWeb.Extensions.ServiceExtensions.Swagger.CustomApiVersion;
using Newtonsoft.Json;
using MyWeb.Business.System;

namespace MyWeb.Api.Controllers.V1.System
{
    /// <summary>
    /// 用户账号管理
    /// </summary>
    [Produces("application/json")]
    [CustomRoute(ApiVersions.V1)]
    public class UserController : BaseController
    {
        private readonly SqlSugarScope _dbBase;

        readonly SysUserInfo _userinfo;

        private readonly IUserService _userService;

        private ISysUserInfoServices _sysUserInfoServices;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="unitOfWorkManage"></param>
        /// <param name="sysUser"></param>
        /// <param name="userService"></param>
        /// <param name="sysUserInfoServices"></param>
        public UserController(IUnitOfWorkManage unitOfWorkManage,SysUserInfo sysUser, IUserService userService, ISysUserInfoServices sysUserInfoServices)
        {
            _dbBase = unitOfWorkManage.GetDbClient();
            _userinfo = sysUser;
            _userService = userService;
            _sysUserInfoServices = sysUserInfoServices;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Get(int page = 1, string key = "")
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            int intPageSize = 50;

            var data = await _userService.Get(page, intPageSize, key);

            return Success(data);
        }

        

        /// <summary>
        /// 获取单个用户信息
        /// </summary>
        /// <param name="UserId">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetUserInfo(long UserId)
        {
            if (UserId == 0L)
            {
                return Failed<SysUserInfo>(null);
            }

            var userInfo = await _userService.GetUserInfo(UserId);

            return Success(userInfo);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sysUserInfo">用户信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> PutUserInfo([FromBody] SysUserInfo sysUserInfo)
        {
            if (sysUserInfo.IsNullOrEmpty())
            {
                return Failed("用户信息不允许为空！");
            }
            if (sysUserInfo.Id.IsNullOrEmpty())
            {
                return Failed("用户信息不允许为空！");
            }

            List<long>? roles = sysUserInfo.RIDs;

            return await _userService.PutUserInfo(sysUserInfo) ? Success("成功") : Failed("更新失败");
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sysUserInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> PostUserInfo([FromBody] SysUserInfo sysUserInfo)
        {
            if (sysUserInfo.LoginName.IsNullOrEmpty())
            {
                return Failed(msg:"用户名为空！");
            }

            return await _userService.PostUserInfo(sysUserInfo) ? Success("成功") : Failed(msg:"更新失败");
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DelUserInfo(long uid)
        {
            if (uid <= 0L)
            {
                return Failed<object>("用户Id不存在！");
            }

            return Success(await _userService.DelUserInfo(uid));
        }


        /// <summary>
        /// 获取用户详情根据token
        /// 【无权限】
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult> GetInfoByToken(string token)
        {
            var data = await _userService.GetInfoByToken(token);
            return Success(data, "获取成功");
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> RemovePwd(long UserId)
        {
            return Success<string>(await _userService.RemovePwd(UserId));
        }

    }
}
